<%@ page contentType="text/html;charset=utf-8"%>
<%@page
	import="com.cs.beneficios.common.ConnectionFactory,com.cs.beneficios.common.entity.Consultas,java.sql.SQLException,java.sql.Connection,java.sql.ResultSet,com.cs.beneficios.common.entity.database"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript"
	src="<%= request.getContextPath() + "/js/popUP.js" %>"></script>
<link href="<%= request.getContextPath() + "/css/bloques.css" %>"
	rel="stylesheet" type="text/css">
<script src="<%=request.getContextPath()%>/js/dateValidation.js"
	type="text/javascript"></script>
<script type="text/javascript"
	src="<%=request.getContextPath() + "/js/jquery-1.8.0.js"%>"></script>
<script type="text/javascript"
	src="<%=request.getContextPath()
					+ "/js/jquery-ui-1.10.3.custom.min.js"%>"></script>
<script type="text/javascript"
	src="<%=request.getContextPath()
					+ "/js/jquery.ui.datepicker-es.js"%>"></script>
<link rel="stylesheet"
	href="<%=request.getContextPath()
					+ "/css/ui-lightness/jquery-ui-1.10.3.custom.css"%>" />
<title>untitled</title>

<script type="text/javascript">
$(document).ready(function() {
	var fechaActual = new Date();
    var dd = fechaActual.getDate();
    var mm = fechaActual.getMonth()+1; // indice 0
    var yyyy = fechaActual.getFullYear();
    if(dd<10){dd='0'+dd;} if(mm<10){mm='0'+mm;} var fechaActual = dd+'/'+mm+'/'+yyyy;
    
	$("#guardarBtn").click(function(){	
	    if ($("#sede").val() == "") {
	    	alert("Debe ingresar el nombre para la sede.");
	        $("#sede").focus();
	        return false;
	    }
	    if ($("#dirEncasillada").val() == "") {
	    	alert("Debe ingresar la direccion encasillada.");
	        $("#viaTipo").focus();
	        return false;
	    }
		$("#mainForm").attr('target','_self');
		var fDesdeValida = isDate($("#desde").val());
		var fHastaValida = isDate($("#hasta").val());
		if(!fDesdeValida){
			alert('Fecha desde vigencia no valida.');
			return false;
		}else if(!fHastaValida){
			alert('Fecha hasta vigencia no valida.');
			return false;
		}else{
			if( $.datepicker.parseDate("dd/mm/yy", $("#desde").val()) < $.datepicker.parseDate("dd/mm/yy", fechaActual)){
				alert('La fecha inicial es menor a la fecha de hoy.');
				return false;
			}
			if( $.datepicker.parseDate("dd/mm/yy", $("#desde").val()) > $.datepicker.parseDate("dd/mm/yy", $("#hasta").val())){
				alert('La vigencia desde no puede ser mayor a la vigencia hasta.');
				return false;
			}
		}
		$("#mainForm").submit();
					
	});
	
});
</script>

<!-- DatePickers -->
<script type="text/javascript">
$.datepicker.setDefaults({
	  showOn: "button",
	  buttonImageOnly: true,
	  buttonImage: "<%=request.getContextPath() + "/images/Calendar.png"%>",
	  buttonText : "Calendario"
	});
	$(function() {
		$("#desde").datepicker({
			changeMonth : true,
			changeYear : true
		});
	});
	$(function() {
		$("#hasta").datepicker({
			changeMonth : true,
			changeYear : true
		});
	});
</script>

<script type="text/javascript">     
function agregar(object,origen){
	$("#dirdescriptiva").val("");
    $("#dirdescriptiva").prop('disabled', true);
    if($("#viaTipo").val() == 0){
    	alert('El tipo no es valido.');
      	return;
    }
    if(origen == 'tipo') tipo = object.value.toUpperCase();
    if(origen == 'vNro'){
    	if(object.value.length == 1) object.value='00'+object.value;
      	if(object.value.length == 2) object.value='0'+object.value;
      	vNro = object.value.toUpperCase();
    }
    if(origen == 'vOr') vOr = object.value.toUpperCase();
    if(origen == 'vApn'){
    	if(object.value.length == 1) object.value=' '+object.value;
      	vApn = object.value.toUpperCase();
    }
    if(origen == 'cOr') cOr = object.value.toUpperCase();
    if(origen == 'cNro'){
    	if(object.value.length == 1) object.value='00'+object.value;
      	if(object.value.length == 2) object.value='0'+object.value;
      	cNro = object.value.toUpperCase();
    }
    if(origen == 'cApn') {
      	if(object.value.length == 1) object.value=' '+object.value;
      	cApn = object.value.toUpperCase();
    }
    if(origen == 'placa'){
      	if(object.value.length == 1) object.value='00'+object.value;
      	if(object.value.length == 2) object.value='0'+object.value;
      	placa = object.value.toUpperCase();
    }
    if(origen == 'interior'){
      	if(object.value.length == 1) object.value='0000'+object.value;
      	if(object.value.length == 2) object.value='000'+object.value;
      	if(object.value.length == 3) object.value='00'+object.value;
      	if(object.value.length == 4) object.value='0'+object.value;
      	interior = object.value.toUpperCase();
    }
    if(origen == 'bloque'){
      	if(object.value.length == 1) object.value='0'+object.value;
      	bloque = object.value;
    }

    $("#dirEncasillada").val(tipo+" "+vOr+vNro+" "+vApn+"   "+cOr+cNro+" "+cApn+" "+placa+" "+bloque+interior);
}
</script>

</head>
<body>
	<P></P>
	<P></P>
	<P align="center"></P>
	<P align="center" class="titulosenblanco">ADMINISTRACION DE SEDES</P>
	<P align="center"></P>
	<DIV align="center">
		<form name="frmIngresarSede" id="mainForm" method="POST"
			action="<%=request.getContextPath()+"/Sedes/GuardarSede.do"%>">
			<input type="hidden" name="codsede" id="codsede" />
			<div align="center">
				<table style="width: 60%; border: 0; padding: 3px; border-spacing: 3px;">
					<tr>
						<td width="6%"></td>
						<td width="35%" class="celdatabla">Nombre (*)</td>
						<td width="68%">
							<input type="text" class="form" name="sede" id="sede" maxlength="70" size="70" />
						</td>
					</tr>
					<tr>
						<td width="6%"></td>
						<td width="35%" class="celdatabla">Subsede</td>
						<td width="68%"><input type="text" Class="form"
							maxlength="40" size="40" name="subsede" id="subsede" readonly /> 
							<a href='javascript:vpopUsers("<%=request.getContextPath()+"/jsp/sedes/Subsede.jsp"%>");'>
								<img border="0"
								src="<%= request.getContextPath() + "/images/sede.GIF" %>"
								alt="Seleccionar una Sede" width="16" height="16" />
							</a>
						</td>
					</tr>
					<tr>
						<td width="6%"></td>
						<td width="35%" class="celdatabla">Descripcion</td>
						<td width="68%">
							<textarea name="descripcion" id="descripcion" class="form"
								cols="40" rows="7">
							</textarea>
						</td>
					</tr>
					<%
  
//WTF. DAS IT MANE.jpg.png.avi.. esto hay que refactorizarlo.
 ConnectionFactory cf = new ConnectionFactory();
 Consultas consultas = new Consultas(cf);
 ResultSet rs = null;
 rs = consultas.getMunicipios();
 
 if(rs!=null)
 %>
					<tr>
						<td width="6%"></td>
						<td width="35%" class="celdatabla">Municipio (*)</td>
						<td width="68%"><select name="municipio" id="municipio">

								<% try {
               while(rs.next()){%>
								<option class="form" value="<%=rs.getString("codigo")%>"><%=rs.getString("nombre")%></option>
								<%}%>
						</select> <% }catch(SQLException e){
               e.printStackTrace();
              } %></td>
					</tr>
					<tr>
						<td width="6%"></td>
						<td width="35%" class="celdatabla">Tipo</td>
						<td width="68%"><input type="text" class="form" name="tipo" id="tipo"
							maxlength="30" size="30" /></td>
					</tr>
					<tr>
						<td width="6%"></td>
						<td width="35%" class="celdatabla">
							C&aacute;racter	&nbsp;&nbsp;
						</td>
						<td width="68%">
							<select name="caracter" id="caracter" class="formLista">
								<option value="Publica">Publica</option>
								<option value="Privada">Privada</option>
								<option value="Otra">Otra</option>
							</select>
						</td>
					</tr>
					<tr>
						<td width="6%"></td>
						<td width="35%" class="celdatabla" colspan="2">
							<P>Direcci&oacute;n Encasillada&nbsp;&nbsp; (*) &nbsp;&nbsp;</P>
						</td>
					</tr>
					<tr>
						<td width="28%">&nbsp;</td>
						<td colspan="3" class="celdatabla">
							<div align="left">
								<table style="width: 48%; border: 1; padding: 2px; border-spacing: 3px;">
									<tr>
										<td colspan="4">
											<DIV align="center">
												<STRONG>Datos de la via</STRONG>
											</DIV>
										</td>
										<td colspan="3">
											<DIV align="center">
												<STRONG>Datos del cruce</STRONG>
											</DIV>
										</td>
										<td colspan="3">
											<DIV align="center">
												<STRONG>Datos adicionales</STRONG>
											</DIV>
										</td>
									</tr>
									<tr>
										<td>
											<DIV align="center">Tipo</DIV>
										</td>
										<td>
											<DIV align="center">Or.</DIV>
										</td>
										<td>
											<DIV align="center">Nro.</DIV>
										</td>
										<td>
											<DIV align="center">Apn.</DIV>
										</td>
										<td>
											<DIV align="center">Or.</DIV>
										</td>
										<td>
											<DIV align="center">Nro.</DIV>
										</td>
										<td>
											<DIV align="center">Apn.</DIV>
										</td>
										<td>
											<DIV align="center">Placa</DIV>
										</td>
										<td>
											<DIV align="center">Bloque</DIV>
										</td>
										<td>
											<DIV align="center">Interior</DIV>
										</td>

									</tr>
									<tr>
										<td>
											<% String tipo = request.getParameter("viaTipo"); %> <select
											size="1" name="viaTipo" id="viaTipo" onchange="agregar(this,'tipo')"
											onmouseover="this.T_WIDTH=180;this.T_TITLE='Tipo de via';return escape('Corresponde al tipo de via.');"
											class="form">
												<% if((Object)tipo != null){
                            					if(tipo.equals("0")){%>
												<option value="0" selected="selected"></option>
												<%}else{%>
												<option value="0"></option>
												<%}%>
												<% if(tipo.equals("CR")){%>
												<option value="CR" selected="selected">Carrera</option>
												<%}else{%>
												<option value="CR">Carrera</option>
												<%}%>
												<% if(tipo.equals("CL")){%>
												<option value="CL" selected="selected">Calle</option>
												<%}else{%>
												<option value="CL">Calle</option>
												<%}%>
												<% if(tipo.equals("CQ")){%>
												<option value="CQ" selected="selected">Circular</option>
												<%}else{%>
												<option value="CQ">Circular</option>
												<%}%>
												<% if(tipo.equals("TR")){%>
												<option value="TR" selected="selected">Transversal</option>
												<%}else{%>
												<option value="TR">Transversal</option>
												<%}%>
												<% if(tipo.equals("DG")){%>
												<option value="DG" selected="selected">Diagonal</option>
												<%}else{%>
												<option value="DG">Diagonal</option>
												<%}%>
												<% if(tipo.equals("AV")){%>
												<option value="AV" selected="selected">Avenida</option>
												<%}else{%>
												<option value="AV">Avenida</option>
												<%}
                          						}else{%>
												<option value="0"></option>
												<option value="CR">Carrera</option>
												<option value="CL">Calle</option>
												<option value="CQ">Circular</option>
												<option value="TR">Transversal</option>
												<option value="DG">Diagonal</option>
												<option value="AV">Avenida</option>
												<%}%>
										</select>
										</td>
										<td>
											<% String vOr = request.getParameter("viaOrientacion"); %> <select
											size="1" class="form" onchange="agregar(this,'vOr')"
											name="viaOrientacion" id="viaOrientacion"
											onmouseover="this.T_WIDTH=180;this.T_TITLE='Orientacion de la via';return escape('Corresponde a la orientacion de la via.');">
												<% if((Object)vOr != null){
                            					if(vOr.equals("0")){%>
												<option value="0" selected="selected"></option>
												<%}else{%>
												<option value="0"></option>
												<%}%>
												<% if(vOr.equals("E")){%>
												<option value="E" selected="selected">Este</option>
												<%}else{%>
												<option value="E">Este</option>
												<%}%>
												<% if(vOr.equals("S")){%>
												<option value="S" selected="selected">Sur</option>
												<%}else{%>
												<option value="S">Sur</option>
												<%}
                   								}else{%>
												<option value="0"></option>
												<option value="E">Este</option>
												<option value="S">Sur</option>
												<%}%>
										</select>
										</td>
										<td>
											<% String vNro = request.getParameter("vNro"); 
                      if((Object)vNro == null) vNro = "";
                   %> <input type="text" value="<%=vNro%>"
											onchange="agregar(this,'vNro')" name="vNro" id="vNro" class="form"
											maxlength="3" size="3"
											onmouseover="this.T_WIDTH=180;this.T_TITLE='Numero de la via';return escape('Corresponde al numero de la via.');" />
										</td>
										<td>
											<% String vAp = request.getParameter("viaApendice"); 
                      if((Object)vAp == null) vAp = "";
                   %> <input type="text" value="<%=vAp%>" class="form"
											size="2" maxlength="2" name="viaApendice" id="viaApendice"
											onchange="agregar(this,'vApn')"
											onmouseover="this.T_WIDTH=180;this.T_TITLE='Apendice de la via';return escape('Corresponde al apendice; ejemplo: A, AA, B, BB etc.');" />
										</td>
										<td>
											<DIV align="center">
												<% String cOr = request.getParameter("cruceOrientacion"); %>
												<select size="1" class="form" name="cruceOrientacion" id="cruceOrientacion"
													onchange="agregar(this,'cOr')"
													onmouseover="this.T_WIDTH=180;this.T_TITLE='Orientacion del cruce';return escape('Corresponde a la orientacion del cruce.');">
													<% if((Object)cOr != null){
                            if(cOr.equals("0")){%>
													<option value="0" selected="selected"></option>
													<%}else{%>
													<option value="0"></option>
													<%}%>
													<% if(cOr.equals("E")){%>
													<option value="E" selected="selected">Este</option>
													<%}else{%>
													<option value="E">Este</option>
													<%}%>
													<% if(cOr.equals("S")){%>
													<option value="S" selected="selected">Sur</option>
													<%}else{%>
													<option value="S">Sur</option>
													<%}
                    }else{%>
													<option value="0"></option>
													<option value="E">Este</option>
													<option value="S">Sur</option>
													<%}%>
												</select>
											</DIV>
										</td>
										<td>
											<DIV align="center">
												<% String cNro = request.getParameter("cruceNumero"); 
                      if((Object)cNro == null) cNro = "";
                      %>
												<input type="text" value="<%=cNro%>" class="form" size="3"
													maxlength="3" name="cruceNumero" id="cruceNumero"
													onchange="agregar(this,'cNro')"
													onmouseover="this.T_WIDTH=180;this.T_TITLE='Numero del cruce';return escape('Primer numero de la placa de la puerta.');" />
											</DIV>
										</td>
										<td>
											<DIV align="center">
												<% String cAp = request.getParameter("cruceApendice"); 
                      if((Object)cAp == null) cAp = "";
                    %>
												<input type="text" value="<%=cAp%>" class="form"
													maxlength="2" size="2" name="cruceApendice" id="cruceApendice"
													onchange="agregar(this,'cApn')"
													onmouseover="this.T_WIDTH=180;this.T_TITLE='Apendice del cruce';return escape('Corresponde al apendice; ejemplo: A, AA, B, BB etc.');" />
											</DIV>
										</td>
										<td>
											<DIV align="center">
												<% String placa = request.getParameter("placa"); 
                      if((Object)placa == null) placa = "";
                    %>
												<input type="text" value="<%=placa%>" size="3" maxlength="3"
													name="placa" id="placa" onchange="agregar(this,'placa')" class="form"
													onmouseover="this.T_WIDTH=180;this.T_TITLE='Placa';return escape('Es el segundo numero de la puerta y que corresponde a la distancia que existe entre la esquina y la propiedad.');" />
											</DIV>
										</td>
										<td>
											<DIV align="center">
												<% String bloque = request.getParameter("bloque"); 
                      if((Object)bloque == null) bloque = "";
                    %>
												<input type="text" value="<%=bloque%>" class="form"
													maxlength="2" name="bloque" id="bloque"
													onchange="agregar(this,'bloque')" size="2"
													onmouseover="this.T_WIDTH=180;this.T_TITLE='Bloque';return escape('Aplica para edificios o unidades residenciales.');" />
											</DIV>
										</td>
										<td>
											<DIV align="center">
												<% String interior = request.getParameter("interior"); 
                      if((Object)interior == null) interior = "";
                     %>
												<input type="text" value="<%=interior%>" class="form"
													size="5" name="interior" id="interior"
													onchange="agregar(this,'interior')" maxlength="5"
													onmouseover="this.T_WIDTH=180;this.T_TITLE='Interior';return escape('El numero de apartamento, si es el caso.');" />
											</DIV>
										</td>

									</tr>
									<tr>
										<td colspan="10">
											<DIV align="center">
												<STRONG>Direccion Generada </STRONG>
											</DIV>
										</td>
									</tr>
									<tr>
										<% String dire = request.getParameter("dirEncasillada"); 
                      if((Object)dire == null) dire = "";
                     %>
										<td colspan="10">
											<DIV align="center">
												<input type="text" name="dirEncasillada" id="dirEncasillada" class="form"
													value="<%=dire%>" size="40">
											</DIV>
										</td>
									</tr>
								</table>
							</div>
						</td>
					</tr>
					<tr>
						<td width="6%"></td>
						<td width="35%" class="celdatabla">
							Direcci&oacute;n &nbsp;Descriptiva&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
						<td width="68%"><input type="text" class="form"
							name="dirdescriptiva" id="dirdescriptiva" maxlength="30" size="30" /></td>
					</tr>
					<tr>
						<td width="6%"></td>
						<td width="35%" class="celdatabla">Localizacion COBAMA
							&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
						<td width="68%"><input type="text" class="form" name="cobama" id="cobama"
							maxlength="25" size="25" /></td>
					</tr>
					<tr>
						<td width="6%"></td>
						<td width="35%" class="celdatabla">Desde Vigencia</td>
						<td width="68%">
							<input name="desde" id="desde" class="form"/>
						</td>
					</tr>

					<tr>
						<td width="6%"></td>
						<td width="35%" class="celdatabla">Hasta Vigencia</td>
						<td width="68%">
							<input name="hasta" id="hasta" class="form" />
						</td>
					</tr>
					<tr>
						<td width="6%"></td>
						<td width="35%"></td>
						<td width="68%"></td>
					</tr>
					<tr>
						<td width="6%"></td>
						<td width="27%">&nbsp;</td>
						<td width="68%">
							<input type="button" value="Guardar" name="guardarBtn" id="guardarBtn" />
						</td>
					</tr>
				</table>
			</div>

			<P>
<script type="text/javascript">    
var tipo = 		$("#viaTipo").val();
var vNro = 		$("#vNro").val();
var vOr = 		$("#viaOrientacion").val();
var vApn = 		$("#viaApendice").val();
var cOr = 		$("#cruceOrientacion").val();
var cNro = 		$("#cruceNumero").val();
var cApn = 		$("#cruceApendice").val();
var placa = 	$("#placa").val();
var interior = 	$("#interior").val();
var bloque = 	$("#bloque").val();              
</script>
				<%
        if(session.getAttribute("mensajes") != null){
          out.println((String)session.getAttribute("mensajes"));
        }
          session.removeAttribute("mensajes");
      %>
			
			<P align="center" class="celdatabla">Los campos marcados con (*)
				son obligatorios</P>
			<P>
				<a href="javascript:window.history.back()"> <img
					src="<%=request.getContextPath() + "/images/BACK1-1.gif"%>"
					border="0" />
				</a>

			</P>
		</form>
	</DIV>
	<P align="center"></P>
	<% cf.closeConnection(consultas.getConn());%>
</body>
</html>
